﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using CSM.Models;

namespace CSM.ashx
{
    public class SaveQData : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            CustomDataContext db = new CustomDataContext();
            context.Response.ContentType = "text/plain";
            string questionid = "";
            string title = "";
            string istrue = "";
            string dataarray = "";
            string editflag = "";
            try
            {
                questionid = context.Request.Form["questionid"];
                title = context.Request.Form["title"];
                istrue = context.Request.Form["istrue"];
                dataarray = context.Request.Form["dataarray"];
                editflag = context.Request.Form["editflag"];
                string[] options = dataarray.Split('|');
                int num = options.Length-1;
                if (editflag == "add")
                {
                    TCrm_WJ_QuestionBase myrow = new TCrm_WJ_QuestionBase();
                    myrow.QuestionID = questionid;
                    myrow.Title = title;
                    myrow.QType = Convert.ToChar(istrue);
                    db.TCrm_WJ_QuestionBase.InsertOnSubmit(myrow);
                    db.SubmitChanges();
                    for (int j = 0; j < num; j++)
                    {
                        TCrm_WJ_QuestionBase_Detail mydetailrow = new TCrm_WJ_QuestionBase_Detail();
                        mydetailrow.OptionID = (j + 1).ToString();
                        mydetailrow.OptionLabel = options[j];
                        mydetailrow.QuestionID = questionid;
                        db.TCrm_WJ_QuestionBase_Detail.InsertOnSubmit(mydetailrow);
                        db.SubmitChanges();
                    }

                }
                else if (editflag == "edit")
                {
                    string myUserId = questionid;
                    var myEditQuery = (from a in db.TCrm_WJ_QuestionBase where a.QuestionID == myUserId select a).Single();
                    myEditQuery.QuestionID = questionid;
                    myEditQuery.Title = title;
                    myEditQuery.QType = Convert.ToChar(istrue);
                    db.SubmitChanges();
                    var queryDel = from a in db.TCrm_WJ_QuestionBase_Detail where a.QuestionID == myUserId select a;
                    db.TCrm_WJ_QuestionBase_Detail.DeleteAllOnSubmit(queryDel);
                    db.SubmitChanges();
                    for (int j = 0; j < num; j++)
                    {
                        TCrm_WJ_QuestionBase_Detail mydetailrow = new TCrm_WJ_QuestionBase_Detail();
                        mydetailrow.QuestionID = questionid;
                        mydetailrow.OptionID = (j + 1).ToString();
                        mydetailrow.OptionLabel = options[j];
                        db.TCrm_WJ_QuestionBase_Detail.InsertOnSubmit(mydetailrow);
                        db.SubmitChanges();
                    }
                }
            }
            catch
            {
                context.Response.Write("error");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
